home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / libs / libelf-0.5 / libelf-0 / libelf-0.5.2 / byteswap.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-19  |  2.5 KB  |  74 lines

  1. /*
  2. byteswap.h - C preprocessor macros for byte swapping.
  3. Copyright (C) 1995 Michael Riepe <riepe@ifwsn4.ifw.uni-hannover.de>
  4.  
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19.  
  20. #ifndef _BYTESWAP_H
  21. #define _BYTESWAP_H
  22.  
  23. #define lu(from,i,s)        (((long)((unsigned char*)(from))[i])<<(s))
  24. #define li(from,i,s)        (((long)((signed char*)(from))[i])<<(s))
  25.  
  26. #define __load_u16L(from)    ((unsigned long)(lu(from,1,8)|lu(from,0,0)))
  27. #define __load_u16M(from)    ((unsigned long)(lu(from,0,8)|lu(from,1,0)))
  28. #define __load_i16L(from)    ((long)(li(from,1,8)|lu(from,0,0)))
  29. #define __load_i16M(from)    ((long)(li(from,0,8)|lu(from,1,0)))
  30.  
  31. #define __load_u32L(from)    ((unsigned long)(lu(from,3,24)|    \
  32.                          lu(from,2,16)|    \
  33.                          lu(from,1,8)|    \
  34.                          lu(from,0,0)))
  35. #define __load_u32M(from)    ((unsigned long)(lu(from,0,24)|    \
  36.                          lu(from,1,16)|    \
  37.                          lu(from,2,8)|    \
  38.                          lu(from,3,0)))
  39. #define __load_i32L(from)    ((long)(li(from,3,24)|    \
  40.                     lu(from,2,16)|    \
  41.                     lu(from,1,8)|    \
  42.                     lu(from,0,0)))
  43. #define __load_i32M(from)    ((long)(li(from,0,24)|    \
  44.                     lu(from,1,16)|    \
  45.                     lu(from,2,8)|    \
  46.                     lu(from,3,0)))
  47.  
  48. #define su(to,i,v,s)        (((char*)(to))[i]=((unsigned long)(v)>>(s)))
  49. #define si(to,i,v,s)        (((char*)(to))[i]=((long)(v)>>(s)))
  50.  
  51. #define __store_u16L(to,v)    (su(to,1,v,8),su(to,0,v,0))
  52. #define __store_u16M(to,v)    (su(to,0,v,8),su(to,1,v,0))
  53. #define __store_i16L(to,v)    (si(to,1,v,8),si(to,0,v,0))
  54. #define __store_i16M(to,v)    (si(to,0,v,8),si(to,1,v,0))
  55.  
  56. #define __store_u32L(to,v)    (su(to,3,v,24),    \
  57.                  su(to,2,v,16),    \
  58.                  su(to,1,v,8),    \
  59.                  su(to,0,v,0))
  60. #define __store_u32M(to,v)    (su(to,0,v,24),    \
  61.                  su(to,1,v,16),    \
  62.                  su(to,2,v,8),    \
  63.                  su(to,3,v,0))
  64. #define __store_i32L(to,v)    (si(to,3,v,24),    \
  65.                  si(to,2,v,16),    \
  66.                  si(to,1,v,8),    \
  67.                  si(to,0,v,0))
  68. #define __store_i32M(to,v)    (si(to,0,v,24),    \
  69.                  si(to,1,v,16),    \
  70.                  si(to,2,v,8),    \
  71.                  si(to,3,v,0))
  72.  
  73. #endif /* _BYTESWAP_H */
  74.